//
//  KDDatePickerView.m
//  警用舆情平台
//
//  Created by 魏强 on 15/5/25.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "KDDatePickerView.h"
#import "KDCover.h"
@interface KDDatePickerView ()
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;//显示时间的label
@property (weak, nonatomic) IBOutlet UIDatePicker *datepicker;//
/**
 *  是那种时间：开始还是结束
 */
@property (nonatomic, assign) KDDatePickerType type;
/**
 *  被选中的时间
 */
@property (nonatomic, strong) NSDate *curDate;
@property (nonatomic, strong) NSDateFormatter *fmt;

@end

@implementation KDDatePickerView
#pragma mark - 懒加载区
//懒加载时期转换器
- (NSDateFormatter *)fmt
{
    if (!_fmt) {
        _fmt = [[NSDateFormatter alloc]init];
        _fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];;
        _fmt.dateFormat = @"yyyy-MM-dd";
    }
    return _fmt;
}
#pragma mark - 生命周期区

#pragma mark - 板块布置区
+ (instancetype)showWithType:(KDDatePickerType)type andDate:(NSDate *)curDate{
    //创建蒙板
    KDCover *cover = [KDCover create];
    //    cover.frame = [UIApplication sharedApplication].keyWindow.bounds;
    //    cover.backgroundColor = GBColor(0, 0, 0, 0.5);
    //    [[UIApplication sharedApplication].keyWindow addSubview:cover];
    KDDatePickerView *pickView = [[[NSBundle mainBundle] loadNibNamed:@"KDDatePickerView" owner:nil options:nil ]lastObject];
    [cover addTarget:pickView action:@selector(hideDatePickerView) forControlEvents:UIControlEventTouchDown];
    
    CGFloat width = 300;
    CGFloat height = 360;
    pickView.frame = CGRectMake((screenW - width)*0.5, (screenH - height) *0.5, width, height);
    pickView.curDate = curDate;
    pickView.datepicker.date = curDate;
    pickView.type = type;
    [pickView refreshLabel];
    [cover addSubview:pickView];
    return pickView;
}

+ (instancetype)showDatePickerViewWithType:(KDDatePickerType)type andDate:(NSDate *)curDate
{
    KDDatePickerView *pickView = [[[NSBundle mainBundle] loadNibNamed:@"KDDatePickerView" owner:nil options:nil ]lastObject];
    pickView.curDate = curDate;
    pickView.datepicker.date = curDate;
    pickView.type = type;
    [pickView refreshLabel];
    return pickView;
}
+ (void)hide{
    //隐藏蒙板
    for (UIView *childView in [UIApplication sharedApplication].keyWindow.subviews) {
        if ([childView.subviews[0] isKindOfClass:self]) {
            [childView removeFromSuperview];
        }
    }
}
- (void)hideDatePickerView
{
    [self.superview removeFromSuperview];
    
}
- (void)refreshLabel
{
    self.dateLabel.text = [self.fmt stringFromDate:self.curDate];
}
#pragma mark - 事件点击区
//取消按钮被点击了
- (IBAction)cancelBtncClick:(id)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(cancelBtnClick)]) {
        [_delegate cancelBtnClick];
    }
    [self.superview removeFromSuperview];
}
//设置按钮被点击了
- (IBAction)settingBtnClick:(id)sender {
    
    
    if (_delegate && [_delegate respondsToSelector:@selector(datePickerView:settingBtnClickWithDate:andType:)]) {
        [_delegate datePickerView:self settingBtnClickWithDate:self.curDate andType:self.type];
    }
    [self.superview removeFromSuperview];
    
}

- (IBAction)dateChange:(UIDatePicker *)datepicker
{
    self.curDate = datepicker.date;
    [self refreshLabel];
}

#pragma mark - 网络请求区

#pragma mark - 通知接收区

#pragma mark - KDCoverDelegate


@end
